package com.elinkway.infinitemovies.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.elinkway.infinitemovies.bean.ChannelInfo;
import com.elinkway.infinitemovies.bean.ChannelInfoList;
import com.le123.ysdq.R;
import com.nostra13.universalimageloader.core.ImageLoader;

public class ChannelHomeListAdapter extends MyBaseAdapter {

	private ChannelInfoList item_data = new ChannelInfoList();

	public ChannelHomeListAdapter(Context context, ChannelInfoList data) {
		super(context);
		this.item_data = data;
		// TODO Auto-generated constructor stub
	}

	public ChannelHomeListAdapter(Context context) {
		// TODO Auto-generated constructor stub
		super(context);
	}

	private class ItemHolder {
		TextView tv_name;
		TextView tv_number;
		TextView tv_intro;
		ImageView img_cover;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		ChannelInfo channel = item_data.getChannels().get(position);
		ItemHolder item;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.channel_item, null);
			item = new ItemHolder();
			item.tv_name = (TextView) convertView
					.findViewById(R.id.channel_item_name);
			item.tv_number = (TextView) convertView
					.findViewById(R.id.channel_item_number);
			item.tv_intro = (TextView) convertView
					.findViewById(R.id.channel_item_intro);
			item.img_cover = (ImageView) convertView
					.findViewById(R.id.channel_item_cover);
			convertView.setTag(item);
		} else {
			item = (ItemHolder) convertView.getTag();
		}
		item.tv_name.setText(channel.getName());
		item.tv_number.setText(String.format(
				mContext.getResources().getString(R.string.channel_total),
				channel.getTotal()));
		if (channel.getTotal() > 0) {
			item.tv_number.setVisibility(View.VISIBLE);
		} else {
			item.tv_number.setVisibility(View.GONE);
		}
		item.tv_intro.setText(channel.getSubName());
		ImageLoader.getInstance()
				.displayImage(channel.getPic(), item.img_cover);
		return convertView;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return item_data.getChannels().size();
	}
}
